package com.financing.platform.activities.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.platform.activities.ActivitiesBusiness;
import com.financing.platform.dao.IActivitieGiftDao;
import com.financing.platform.dao.IActivitiesMollionsWealthDao;
import com.financing.platform.dao.IFaUserDao;
import com.financing.platform.dao.IOrderDao;
import com.financing.platform.dao.IUserAccountDao;
import com.financing.platform.dto.ActivitesMollionsWealthDTO;
import com.financing.platform.dto.ActivitieGiftDTO;
import com.financing.platform.dto.FaUserDTO;
import com.financing.platform.dto.OrderDTO;
import com.financing.platform.dto.UserAccountDetailDTO;
import com.financing.platform.util.Dict;

@Service("activitiesBWCFJHImpl")
public class ActivitiesBWCFJHImpl implements ActivitiesBusiness{

	protected Logger log =Logger.getLogger(this.getClass());
	
	@Autowired
	private IActivitieGiftDao activitieGiftDaoImpl;
	
	@Autowired
	private IActivitiesMollionsWealthDao activitiesMollionsWealthDaoImpl;
	
	@Autowired
	private IUserAccountDao userAccountDaoImpl;
	
	@Autowired
	private IFaUserDao faUserDaoImpl;
	
	@Autowired
	private IOrderDao orderDaoImpl;
	
	@SuppressWarnings("unchecked")
	@Override
	public void handleOrderRebate(Object object) throws Exception{
		HashMap<String,Object> map =(HashMap<String, Object>)object;
		
		OrderDTO orderDTO = (OrderDTO)map.get("orderDTO");
		OrderDTO orderTmp = (OrderDTO)map.get("orderTmp");
		
		if(Dict.order_status_rebate == orderDTO.getoStatus()){
			ActivitesMollionsWealthDTO activitiesMollionsWealthDTO = new ActivitesMollionsWealthDTO();
			activitiesMollionsWealthDTO.setpId(orderTmp.getoProjectId());
			activitiesMollionsWealthDTO.setBeginTime(orderTmp.getCreateDate());
			activitiesMollionsWealthDTO.setEndTime(orderTmp.getCreateDate());
			
			//查询当前订单产品是否有活动
			List<ActivitesMollionsWealthDTO> listActivitesMollionsWealth = activitiesMollionsWealthDaoImpl.queryActivitiesMollionsWealthProduct(activitiesMollionsWealthDTO);
			
			if(listActivitesMollionsWealth!=null && listActivitesMollionsWealth.size() == 1){
				activitiesMollionsWealthDTO = listActivitesMollionsWealth.get(0);
				
				//判断是否满足送的条件
				if(isNeedAddAccount(orderTmp,activitiesMollionsWealthDTO)){
					//添加红包记录状态为打开
					int giftId = addGift(orderDTO,orderTmp, activitiesMollionsWealthDTO);
					//添加账户金额变动流水
					addAccountDetail(orderDTO,orderTmp, activitiesMollionsWealthDTO,giftId);
					
					//增加账户金额
					updateUserAccount(orderDTO, orderTmp, activitiesMollionsWealthDTO);
				}
				
			}
		}
		
	}
	
	/**
	 * 
	 * @Title: isNeedAddAccount
	 * @Description: TODO 
	 * @param @return   
	 * @return boolean   
	 * @throws
	 */
	private boolean isNeedAddAccount(OrderDTO orderTmp,ActivitesMollionsWealthDTO activitiesMollionsWealthDTO){
		
		//如果是新手产品 fa第一笔订单送
		if("1".equals(activitiesMollionsWealthDTO.getActivitiesProductType())){
			OrderDTO orderDTO = new OrderDTO();
			orderDTO.setoFAId(orderTmp.getoFAId());
			List<OrderDTO> orderList = orderDaoImpl.queryOrderList(orderDTO);
			int k = 0;
			
			//
			for(OrderDTO order:orderList){
				if(order.getoStatus()>=320){
					k++;
				}
			}
			if(k < 2){
				return true;
			}else{
				return false;
			}
		}
		//如果是认证产品 fa认证后才送
		if("2".equals(activitiesMollionsWealthDTO.getActivitiesProductType())){
			FaUserDTO fauserDTO = new FaUserDTO();
			fauserDTO.setId(orderTmp.getoFAId());
			fauserDTO = faUserDaoImpl.getFaUser(fauserDTO);
			
			if(fauserDTO!=null && !"1".equals(fauserDTO.getuRole())&& !"99".equals(fauserDTO.getuRole())&& !"9".equals(fauserDTO.getuRole())&& !"98".equals(fauserDTO.getuRole())){
				return true;
			}else{
				return false;
			}
		}
		//其他需要奖励产品 当前产品都送
		if("3".equals(activitiesMollionsWealthDTO.getActivitiesProductType())){
			return true;
		}
		
		return false;
	}
	
	/**
	 * 
	 * @Title: addAccountDetail
	 * @Description: 添加账户金额变动流水 
	 * @param @param orderDTO
	 * @param @param orderTmp
	 * @param @param activitiesMollionsWealthDTO
	 * @param @param giftId
	 * @param @return
	 * @param @throws Exception   
	 * @return int   
	 * @throws
	 */
	private int addAccountDetail(OrderDTO orderDTO,OrderDTO orderTmp,ActivitesMollionsWealthDTO activitiesMollionsWealthDTO,int giftId) throws Exception{
		UserAccountDetailDTO userAccountDetailDTO = new UserAccountDetailDTO();
		
		userAccountDetailDTO.setAccountId(userAccountDaoImpl.getUserAccountId(orderTmp.getoFAId()));
		userAccountDetailDTO.setAmount(activitiesMollionsWealthDTO.getAmount());
		userAccountDetailDTO.setActivitieGiftId(giftId);
		userAccountDetailDTO.setFaId(orderTmp.getoFAId());
		userAccountDetailDTO.setAccountType(Dict.P_ACCOUNT_DETAIL_TYPE_INPUT);
		userAccountDetailDTO.setOperateType(Dict.P_ACCOUNT_OPERATE_ACTIVITY);
		userAccountDetailDTO.setOperateStatus(Dict.P_ACCOUNT_OPERATE_STATUS_SUCCESS);
		return userAccountDaoImpl.insertUserAccountDetail(userAccountDetailDTO);
	}
	
	/**
	 * 
	 * @Title: addGift
	 * @Description: 添加红包记录状态为打开 
	 * @param @param orderDTO
	 * @param @param orderTmp
	 * @param @param activitiesMollionsWealthDTO
	 * @param @return   
	 * @return int   
	 * @throws
	 */
	private int addGift(OrderDTO orderDTO,OrderDTO orderTmp,ActivitesMollionsWealthDTO activitiesMollionsWealthDTO) throws Exception{
		ActivitieGiftDTO gift = new ActivitieGiftDTO();
		gift.setaAmount(activitiesMollionsWealthDTO.getAmount());
		gift.setActiviesCode("BWCFJH");
		gift.setpId(orderTmp.getoProjectId());
		gift.setChildActivitiesId(activitiesMollionsWealthDTO.getId());
		gift.setaStatus("02");
		gift.setaType("02");
		
		
		//如果产品是新手专享产品
		if("1".equals(activitiesMollionsWealthDTO.getActivitiesProductType())){
			gift.setaSource("03");
		}
		//如果产品是认证专享
		if("2".equals(activitiesMollionsWealthDTO.getActivitiesProductType())){
			gift.setaSource("04");
		}
		//如果产品为不限
		if("3".equals(activitiesMollionsWealthDTO.getActivitiesProductType())){
			gift.setaSource("05");
		}
		gift.setFaUserId(orderTmp.getoFAId());
		gift.setoId(orderTmp.getId());
		gift.setCreateBy(orderDTO.getUpdateBy());
		gift.setUpdateBy(orderDTO.getUpdateBy());
		gift.setUpdateDate(new Date());
		gift.setCreateDate(new Date());
		return activitieGiftDaoImpl.addActivitieGift(gift);
	}
	
	/**
	 * 
	 * @Title: updateUserAccount
	 * @Description: 修改用户账户金额 
	 * @param @param orderDTO
	 * @param @param orderTmp
	 * @param @param activitiesMollionsWealthDTO
	 * @param @throws Exception   
	 * @return void   
	 * @throws
	 */
	private void updateUserAccount(OrderDTO orderDTO,OrderDTO orderTmp,ActivitesMollionsWealthDTO activitiesMollionsWealthDTO) throws Exception{
		ActivitieGiftDTO gift = new ActivitieGiftDTO();
		gift.setFaUserId(orderTmp.getoFAId());
		gift.setaAmount(activitiesMollionsWealthDTO.getAmount());
		gift.setUpdateBy(orderDTO.getUpdateBy());
		gift.setUpdateDate(new Date());
		userAccountDaoImpl.addAccountMount(gift);
	}

	@Override
	public void handleProductFound(Object object) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void handleFaUserRegister(Object object)  throws Exception{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void handleFaUserAuth(Object object) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
